<template>
  <div>
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-set-default
      @save="saveAddress"
      @change-area="changeArea"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
    />
  </div>
</template>

<script>
import areaList from '@/utils/area'
import { reqAddUserAddress } from '@/api/user'
export default {
  data() {
    return {
      areaList,
      areaCode: '',
    }
  },
  methods: {
    changeArea(areaArr) {
      // 拼接接口所需要的编号格式： 420000-420800-420881
      this.areaCode = areaArr.map((item) => item.code).join('-')
    },
    async saveAddress(addressInfo) {
      addressInfo.areaCode = this.areaCode
      let { status, message } = await reqAddUserAddress(addressInfo)
      this.$toast(message)
      if (status === 0) {
        // 回到上一个页面
        this.$router.back()
      }
    },
  },
}
</script>
